package com.glodon.paas.search.query;

import static org.elasticsearch.index.query.FilterBuilders.*;
import static org.elasticsearch.index.query.QueryBuilders.*;

import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.FilterBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.glodon.paas.search.model.IndexRevitStructure;
import com.glodon.paas.search.model.QueryRevitStructure;

/**
 * 查询服务
 * 
 * @author botao.liao
 * 
 */
@Component("revitQueryService")
public class RevitQueryService extends AbstractQueryService<IndexRevitStructure, QueryRevitStructure> {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Value("${search.revit.fields}")
    private String fields;

    @Override
    public String getFields() {
        return fields;
    }

    public void setFields(String fields) {
        this.fields = fields;
    }

    @Override
    public QueryBuilder prepareQueryBuilder(QueryRevitStructure file) {
        BoolQueryBuilder query = boolQuery();
        if (StringUtils.isNotEmpty(file.getId())) {
            query.must(termQuery("id", file.getId()));
        }
        if (StringUtils.isNotEmpty(file.getDatabagId())) {
            query.must(termQuery("databagId", file.getDatabagId()));
        }
        if (StringUtils.isNotEmpty(file.getName())) {
            query.must(queryString(file.getName()).field("name"));
        }
        if (StringUtils.isNotEmpty(file.getParentId())) {
            query.must(termQuery("parentId", file.getParentId()));
        }
        if (StringUtils.isNotEmpty(file.getPath())) {
            query.must(prefixQuery("path", file.getPath().trim()));
        }
        String structure = file.getStructure();
        if (StringUtils.isNotEmpty(structure)) {
            String[] keyValues = StringUtils.split(structure, "=");
            if (keyValues.length == 1) {// 只有key
                query.must(nestedQuery(
                        "structures",
                        boolQuery().should(queryString(keyValues[0].trim()).field("structures.key")).should(
                                queryString(keyValues[0].trim()).field("structures.value"))));
            } else if (keyValues.length == 2) {// key,value
                query.must(nestedQuery(
                        "structures",
                        boolQuery().must(
                        queryString(keyValues[0].trim()).field("structures.key")).must(
                        queryString(keyValues[1].trim()).field("structures.value"))));
            } else {// 无效的形式
                logger.warn("invalid input revit query:", structure);
            }
        }
        if (StringUtils.isNotEmpty(file.getParentId())) {
            query.must(termQuery("projectId", file.getProjectId()));
        }
        if (file.getCreateTimeFrom() != null && file.getCreateTimeTo() != null) {
            query.must(rangeQuery("createTime").from(file.getCreateTimeFrom()).to(file.getCreateTimeTo()));
        }
        if (file.getUpdateTimeFrom() != null && file.getUpdateTimeTo() != null) {
            query.must(rangeQuery("updateTime").from(file.getUpdateTimeFrom()).to(file.getUpdateTimeTo()));
        }
        return query;
    }

    @Override
    public FilterBuilder prepareFilterBuilder(QueryRevitStructure file) {
        if (StringUtils.isNotEmpty(file.getOwnerId())) {
            return orFilter().add(termFilter("ownerId", file.getOwnerId()))
                    .add(termFilter("shares", file.getOwnerId()));
        }
        return null;
    }

    @Override
    public IndexRevitStructure createIndexObject() {
        return new IndexRevitStructure();
    }
}
